package xyz.hujf.otherbase;

import java.util.concurrent.*;

/**
 * @author Hujf
 * @title: Main
 * @date 2021-04-2310:47
 * @description: TODO
 */
public class Main {
    private static int num = 0;
    private static CountDownLatch count = new CountDownLatch(10);
    public static void main(String[] args) throws InterruptedException {
        //     ThreadNotSafe notSafe = new ThreadNotSafe();
//        Thread a = new Thread(()->{
//            notSafe.setValue(15);
//            System.out.println("a "+notSafe.getValue());
//        });
//
//        Thread b = new Thread(()->{
//            System.out.println("b "+notSafe.getValue());
//        });
//
//
//        Thread c = new Thread(()->{
//            notSafe.setValue(20);
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("c "+notSafe.getValue());
//
//        });
//
//      //  a.start();
//        c.start();
//        b.start();

//        Thread [] t = new Thread[10];
//        for(int i=0;i<t.length;i++){
//            t[i] = new Thread(()->{
//                for(int j=0;j<1000;j++){
//                    inc();
//                }
//            });
//            t[i].start();
//        }

       // ExecutorService servicePool = Executors.newFixedThreadPool(10);

        ExecutorService servicePool = new ThreadPoolExecutor(4,10,1L,TimeUnit.SECONDS,new LinkedBlockingQueue<>(),Executors.defaultThreadFactory());

        for (int i = 0; i < 10; i++) {
            servicePool.execute(() -> {
                for (int j = 0; j < 1000; j++) {
                    inc();
                }
                count.countDown();
            });
        }

        count.await();
        System.out.println(num);
        servicePool.shutdown();
    }


    private synchronized static void inc() {
        num++;
    }


}
